{% extends 'xadmin/base.html' %}
{% load staticfiles %}

{% block title %}
    文章组管理
{% endblock %}

{% block css %}
    <link href="{% static 'xadmin/css/plugins/toastr/toastr.min.css' %}" rel="stylesheet">
{% endblock %}


{% block main %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-lg-3 col-lg-offset-9">
                {% if request.user.is_superuser %}
                    <button type="button" class="btn btn-xs btn-primary" onclick="window.location.href='{% url 'articlegroupadd' %}?siteid={{ siteid }}'"><i class="fa fa-plus"></i>新增文章组</button>
                    <button type="button" class="btn btn-xs btn-danger" onclick="DeleteAll()"><i class="glyphicon glyphicon-remove"></i>批量删除</button>
                {% else %}
                    {% for perm_type, perm_slug_list in perm_dic.items %}
                        {% if perm_type == 'articlegroup' %}
                            {% if 'add' in perm_slug_list %}
                                <button type="button" class="btn btn-xs btn-primary" onclick="window.location.href='{% url 'articlegroupadd' %}?siteid={{ siteid }}'"><i class="fa fa-plus"></i>新增文章组</button>
                            {% endif %}
                            {% if 'delete' in perm_slug_list %}
                                <button type="button" class="btn btn-xs btn-danger" onclick="DeleteAll()"><i class="glyphicon glyphicon-remove"></i>批量删除</button>
                            {% endif %}
                        {% endif %}
                    {% endfor %}
                    
                {% endif %}
                
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content" style="border-width:0px 0px">
                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>

                                            <input type="checkbox" class="i-checks checkall" name="input[]" style="position: absolute; opacity: 0;">

                                        </th>
                                        <th>名称</th>
                                        <th>索引</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for articlegroup in articlegroup_list %}
                                        <tr>
                                            <td>

                                                    <input type="checkbox" class="i-checks" name="input[]" value="{{ articlegroup.id }}" style="position: absolute; opacity: 0;">

                                            </td>
                                            <td>{{ articlegroup.name }}</td>
                                            <td>{{ articlegroup.index_name | default_if_none:"" }}</td>
                                            <td>
                                                {% if request.user.is_superuser %}
                                                    <a  class="btn btn-xs btn-outline btn-primary"  href="{% url 'articlegroupedit' articlegroup.id %}?siteid={{ siteid }}">编辑</a>
                                                    <a  class="btn btn-xs btn-outline btn-warning" onclick="articlegroup_del({{ articlegroup.id }})">删除</a>
                                                {% else %}
                                                    {% for perm_type, perm_slug_list in perm_dic.items %}
                                                        {% if perm_type == 'articlegroup' %}
                                                            {% if 'update' in perm_slug_list %}
                                                                <a  class="btn btn-xs btn-outline btn-primary"  href="{% url 'articlegroupedit' articlegroup.id %}?siteid={{ siteid }}">编辑</a>
                                                            {% endif %}
                                                            {% if 'delete' in perm_slug_list %}
                                                                <a  class="btn btn-xs btn-outline btn-warning" onclick="articlegroup_del({{ articlegroup.id }})">删除</a>
                                                            {% endif %}
                                                        {% endif %}
                                                    {% endfor %}

                                                {% endif %}

                                            </td>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block script %}
    <script src="{% static 'xadmin/js/plugins/toastr/toastr.min.js' %}"></script>
    <script src="{% static 'xadmin/js/myjs/toastr_customer.js' %}"></script>
    <script>
        $(document).ready(function () {
            $('.i-checks').iCheck({
                checkboxClass: 'icheckbox_square-green dcheckbox',
                radioClass: 'iradio_square-green',
            });
            $('.checkall').on('ifChecked', function(event){
                $('.i-checks:not(:first)').iCheck('check');
            });
            $('.checkall').on('ifUnchecked', function(event){
                $('.i-checks:not(:first)').iCheck('uncheck');
            });
        });

        function articlegroup_del(articlegroupid){
            $.ajax({
                url: '{% url 'articlegroupdel' %}',
                type: 'post',
                dataType: 'json',
                data: {'articlegroupid': articlegroupid, 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                success: function(bk){
                    console.log(bk);
                    if(bk.status == '200'){
                        send_success_message(bk.message);
                    }else{
                        toastr.error(bk.message);
                    }
                }
            })
        }
        //批量操作
        function DeleteAll (){
            var id_list = [];
            $('.i-checks:not(:first)').each(function(){
                if($(this).get(0).checked){
                    id_list.push($(this).val());
                }
            });
            if(id_list.length > 0){
                layer.confirm('将删除选中的文章组, 是否继续?', {icon: 3}, function(index){
                    layer.close(index);
                    $.ajax({
                        url: '{% url 'atriclegroupsdel' %}',
                        type: 'post',
                        dataType: 'json',
                        data: {'ids': id_list.join(','), 'csrfmiddlewaretoken': '{{ csrf_token }}', 'siteid': {{ siteid }}},
                        success:function(bk){
                            if(bk.status == '200'){
                                send_success_message(bk.message);
                            }
                            else{
                                toastr.error(bk.message);
                            }
                        }
                    });
                })
            }else{
                toastr.error('请选择要删除的组');
            }
        }
    </script>

{% endblock %}